<template>
    <div>
        <!-- 搜索表单 -->
        <el-form :inline="true" :model="searchFrom" ref="searchFrom" class="demo-form-inline" style="text-align:center">
            <el-form-item label="编号" prop="id">
                <el-input v-model="searchFrom.id" placeholder="编号"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="searchBtn()">查询</el-button>
                <el-button type="warning" icon="el-icon-refresh" @click="resetForm()">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="id" label="编号"/>
            <el-table-column prop="name" label="广告位置名称"/>
            <el-table-column prop="descrition" label="广告描述"/>
            <el-table-column prop="maxCount" label="最多广告数"/>
            <el-table-column align="right">
                <template slot="header">
                    <span style="margin-right:30px">操作</span>
                    <el-button size="mini" type="primary" @click="openAddDialog()">添加</el-button>
                </template>
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="block" style="text-align:center">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[1, 2, 5, 10]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <!-- 添加/修改弹出框 -->
        <el-dialog title="广告位置信息" :visible.sync="dialogAdd">
            <el-form :model="position" ref="addPosition" :rules="rules">
                <el-input v-model="position.id" style="display: none"/>
                <el-form-item label="广告位置名称" label-width="120px" prop="name">
                    <el-input v-model="position.name" ></el-input>
                </el-form-item>
                <el-form-item label="广告描述" label-width="120px" prop="descrition">
                    <el-input v-model="position.descrition" ></el-input>
                </el-form-item>
                <el-form-item label="最多广告数" label-width="120px" prop="maxCount">
                    <el-input-number v-model="position.maxCount" autocomplete="off"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAdd=false">取 消</el-button>
                <el-button type="primary" @click="addPositionBtn()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                tableData: [],
                total: 0, // 总条数
                pageNum: 1, // 当前页数
                pageSize: 5, // 每页显示几条
                dialogAdd: false,
                searchFrom: {},
                position: {},  // 添加和编辑的时候的通用代理商信息
                rules: {
                    type : [
                        {required: true, message: '请输入业务类型', trigger: 'blur'},
                    ],
                    name: [
                        {required: true, message: '请输广告位置名称', trigger: 'blur'}
                    ],
                    descrition: [
                        {required: true, message: '请输入广告描述', trigger: 'blur'},
                    ],
                    maxCount:[
                        {required: true, message: '请输入最大广告数', trigger: 'blur'},
                    ],
                },
            }
        },
        methods: {
            // 重置表格
            resetForm() {
                this.searchFrom={};
                this.getAllPosition();
            },
            // 添加按钮
            openAddDialog() {
                // 设置添加的初始值
                this.position = {};
                this.dialogAdd = true;
            },
            // 打开编辑操作
            handleEdit(index, row) {
                this.dialogAdd = true // 打开弹出层
                // 让表单重新赋值
                this.position = JSON.parse(JSON.stringify(row))
            },
            //每页显示几条改变
            handleSizeChange(val) {
                this.pageSize = val;
                this.getAllPosition();
            },
            // 当前是第几页改变
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getAllPosition();
            },
            // 查询表单提交
            searchBtn() {
                this.pageNum = 1;
                this.getAllPosition();
            },
            // 获得表格数据
            getAllPosition() {
                this.$http.post('mk/advertisePosition/queryAll?pageSize=' + this.pageSize + '&pageNo=' + this.pageNum, this.searchFrom)
                    .then(res => {
                        this.tableData = res.data.t.list;
                        this.total  = res.data.t.total
                    })
            },
            // 添加/修改表单提交
            addPositionBtn() {
                this.$refs["addPosition"].validate((valid) => {
                    if (valid){
                        if (this.position.id === undefined){
                            this.$http.post("mk/advertisePosition/insert", this.position).then(res => {
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '恭喜你，添加成功'});
                                    this.dialogAdd = false;
                                    this.getAllPosition();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })
                        }else {
                            this.$http.put("mk/advertisePosition/update", this.position).then(res => {
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '恭喜你，修改成功'});
                                    this.dialogAdd = false;
                                    this.getAllPosition();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })
                        }

                    }
                })
            },
            // 删除
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', center: true
                }).then(() => {
                    this.$http.delete("mk/advertisePosition/deleteById?advertisePositionId=" + row.id).then(res => {
                        if (Number(res.data.resultCode) === 2000) {
                            this.$message({type: 'success', message: '删除成功!'});
                            this.getAllPosition();
                        } else {
                            this.$message({type: 'error', message: '系统异常'});
                        }
                    })
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                });
            }
        },
        mounted() {
            this.getAllPosition();
        }
    }
</script>

<style scoped>

</style>